package com.mc.web;

import org.apache.log4j.Logger;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;



import com.mc.model.Path;
import com.mc.model.Teacher;
import com.mc.model.Teteacherytpe;
import com.mc.model.Xy;
import com.mc.model.XyType;
import com.mc.model.Xyjb;
import com.mc.model.newsandbulletin;
import com.mc.service.IXXydService;
import com.mc.service.IXyjbService;
import com.mc.service.IXytypeService;
import com.mc.service.IXyxyService;
import com.mc.service.TeacherService;
import com.mc.service.TeteachertypeService;
import com.mc.serviceImpl.XxyyServiceImpl;
import com.mc.serviceImpl.XyjbServiceImpl;
import com.mc.serviceImpl.XytypeServiceImpl;
import com.mc.utils.PathUtil;
import com.mc.utils.StringUtil;
import com.mc.utils.StringUtil2;

@Controller
@RequestMapping("/backend")
public class xyBackend {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(xyBackend.class);
    
	
	@Autowired private IXytypeService xytypeServiceImpl;
	@Autowired private IXyjbService xyjbServiceImpl;
	@Autowired private TeteachertypeService teteachertypeService;
	@Autowired private TeacherService teacherService;
	@Autowired private IXyxyService iXyxyService;  
	public Path path = null;
	/**
	 * 
	 * @param modelmap
	 * @return 添加校友界面
	 */
	@RequestMapping("/addxyui")
	public String addxyui(ModelMap modelmap){
		List<XyType> xyTypes = xytypeServiceImpl.selectallxytype();
		List<Xyjb> xyjbs = xyjbServiceImpl.selectallxyjb();
		List<Teteacherytpe> teteacherytpes = teteachertypeService.selectAllsteacher();
		modelmap.addAttribute("xyTypes", xyTypes);
		modelmap.addAttribute("xyjbs", xyjbs);
		modelmap.addAttribute("teteacherytpes", teteacherytpes);
		path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:addtxy","ViewTitle:添加校友","smenu:xy","mmenu:addxy"});				
		return PathUtil.returnStr(path, modelmap);
	}
	/**
	 * 
	 * @param xy 校友信息
	 * @param xytype 校友类型
	 * @param xyjb  校友级别
	 * @param teacher 导师
	 * @param xyJs 校友届数
	 * @param introduce 简介
	 * @param modelMap
	 * @param files 头像
	 * @return
	 */
	@RequestMapping("/addxy")
	public String addxy(Xy xy,@RequestParam("xytype")int xytype,@RequestParam("xyjb")int xyjb,
			@RequestParam("teacher")int teacher,@RequestParam("xyJs")String xyJs,
			@RequestParam("introduce")String introduce,ModelMap modelMap,@RequestParam(value = "fileste", 
			required = false) MultipartFile [] files){
		String imagePaths=uploadFiels(files);
		XyType xyType2 = xytypeServiceImpl.selectByPrimaryKey(xytype);
		Xyjb xyjb2 = xyjbServiceImpl.selectByPrimaryKey(xyjb);
		Teacher teacher1 = teacherService.selectByPrimaryKey(teacher);
		xy.settId(teacher1);
		xy.setXyIntro(introduce);
		xy.setXyJb(xyjb2);
		xy.setXytId(xyType2);
		xy.setXyJs(xyJs);
		xy.setXyImg(imagePaths);
		iXyxyService.insert(xy);
		//logger.info(xy.toString());
		return "redirect:/backend/xxydmagui_1/1";
		}
	/**
	 * 
	 * @param modelmap
	 * @param pagenow 当前页
	 * @param type 类型
	 * @return 校友管理
	 */
	@RequestMapping("/xxydmagui_{pagenow}/{type}")
	public String xxydmagui(ModelMap modelmap,@PathVariable("pagenow")int pagenow,@PathVariable("type")int type){
		int pnewscount = iXyxyService.countofxybys(type);
		//System.out.println("count---" + pnewscount);
		int pagesize = 10;
		int pagecount = getPageCount(pnewscount, pagesize);
		//System.out.println(pagecount);
		modelmap.addAttribute("pagenow",pagenow);
		modelmap.addAttribute("pagecount",pagecount);
		List<Xy> xys = iXyxyService.selectallxy(type,(pagenow-1)*pagesize, pagesize);
		modelmap.addAttribute("xys", xys);
		path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:xymg","ViewTitle:管理校友","smenu:xy","mmenu:magxy"});				
		return PathUtil.returnStr(path, modelmap);
	}
	public int getPageCount(int allcount,int pagesize)
	{
		//System.out.println("allcount---" + allcount + "--pagesize----");
		int pagecount = 0;
		if(allcount%pagesize == 0)
		{
			pagecount = allcount/pagesize;
		}else{
			pagecount = allcount/pagesize + 1;
		}
		return pagecount;
	}
	/**
	 * 
	 * @param modelMap
	 * @param id 需要更新的校友id
	 * @return 更新校友的界面
	 */
	@RequestMapping("/updatexy/{id}")
	public String updatexy(ModelMap modelMap,@PathVariable("id") int id){
		Xy xy = iXyxyService.selectByPrimaryKey(id);
		//logger.info(xy);
		if(xy.getXyIntro()!=null)
			xy.setXyIntro(StringUtil.getInstace().zy(xy.getXyIntro()));
		modelMap.addAttribute("xy", xy);
		List<XyType> xyTypes = xytypeServiceImpl.selectallxytype();
		List<Xyjb> xyjbs = xyjbServiceImpl.selectallxyjb();
		List<Teteacherytpe> teteacherytpes = teteachertypeService.selectAllsteacher();
		modelMap.addAttribute("xyTypes", xyTypes);
		modelMap.addAttribute("xyjbs", xyjbs);
		modelMap.addAttribute("teteacherytpes", teteacherytpes);
		path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:updatexy","ViewTitle:修改校友信息","smenu:xy","mmenu:addxy"});				
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param xy 校友
	 * @param xytype 校友类型
	 * @param xyjb 校友级别
	 * @param teacher 导师
	 * @param xyJs 校友届数
	 * @param modelMap 
	 * @param files 校友头像
	 * @return 校友管理界面
	 */
	@RequestMapping("/updatexyxx")
	public String updatexyxx(Xy xy,@RequestParam("xytype")int xytype,@RequestParam("xyjb")int xyjb,
			@RequestParam("teacher")int teacher,@RequestParam("xyJs")String xyJs
			,ModelMap modelMap,@RequestParam(value = "fileste", 
			required = false) MultipartFile [] files){
		String imagePaths=uploadFiels(files);
		Xy xy1 =new Xy();
		//logger.info(imagePaths);
		if(!imagePaths.equals("") && imagePaths.length()!=0)
		{
			//logger.info("----test");
			xy.setXyImg(imagePaths);
		}else{
			
			xy1 =iXyxyService.selectByPrimaryKey(xy.getXyId());
			// logger.info(teacher1.toString());
			 xy.setXyImg(xy1.getXyImg());
		}
		XyType xyType2 = xytypeServiceImpl.selectByPrimaryKey(xytype);
		Xyjb xyjb2 = xyjbServiceImpl.selectByPrimaryKey(xyjb);
		Teacher teacher1 = teacherService.selectByPrimaryKey(teacher);
		xy.settId(teacher1);
		xy.setXyJb(xyjb2);
		xy.setXytId(xyType2);
		xy.setXyJs(xyJs);
		int result = iXyxyService.updateByPrimaryKeyWithBLOBs(xy);
		if(result!=0){
		   modelMap.addAttribute("update", true);
		}
		return "forward:/backend/xxydmagui_1/1";
	}
	/**
	 * 
	 * @param files 要进行上传的图片文件
	 * @return 上传照片的路径
	 */
		public String uploadFiels(MultipartFile[] files) {
			String imagepath = "";
			for (MultipartFile file : files) {
				if (file.isEmpty())
					continue;
				else {
					imagepath = com.mc.utils.UploadUtil.getInstance().upload(
							file,
							StringUtil2.getInstance()
									.getPhysicsPath("/images/books"));
					
				}
			}
			return imagepath;
		}
		/**
		 * 
		 * @param id 校友id
		 * @param modelMap
		 * @param request
		 * @return ajax删除校友
		 */
		@RequestMapping("/delxy")
		@ResponseBody
		 public String delmajor(@RequestParam("id") int id,ModelMap modelMap,
				 HttpServletRequest request)
		 {
			 // logger.info(id);
			 int resultCode = iXyxyService.deleteByPrimaryKey(id);
			// System.out.println(resultCode);
			 String callbackFunName = request.getParameter("callbackparam");
			 if(resultCode !=0)
			 {
				 
				 return callbackFunName +"("+ "{'success':true}" +")";
			 }else{
				 //modelMap.addAttribute("delFile", false);
				 return callbackFunName +"("+ "{'success':false}" +")";
			 }
			 
			// return "forward:/backend/ZiliaoManeger/1";
		 }
		
}
